package com.jrx.anytxn.reporting.job.lsFile.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.lsFile.step.*;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 生成账户信息文件 （全量抽取，结清的第一次抽取，后面的不抽取）
 *
 * @author liuyang
 * @date 2021-03-13
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateLSAccountFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(GenerateLSAccountFileConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private LSAccountFileListener accountFileListener;
    @Autowired
    private LSAccountFileProcessor accountFileProcessor;
    @Autowired
    private LSAccountFileWriter accountFileWriter;

    @Bean
    public Job generateLSAccountFileJob() {
        return jobBuilderFactory.get("generateLSAccountFileJob").incrementer(new RunIdIncrementer()).listener(accountFileListener)
                .start(generateNormalLSAccountFileStep())
                .next(generatePayoffLSAccountFileStep())
                .build();
    }

    @Bean
    public Step generateNormalLSAccountFileStep() {
        return stepBuilderFactory.get("generateNormalLSAccountFileStep")
                .<String, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                .reader(accountNormalLSFileReader())
                .processor(accountFileProcessor)
                .writer(accountFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())
                .build();
    }

    @Bean
    public Step generatePayoffLSAccountFileStep() {
        return stepBuilderFactory.get("generatePayoffLSAccountFileStep")
                .<String, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                .reader(accountPayoffLSFileReader())
                .processor(accountFileProcessor)
                .writer(accountFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<String> accountNormalLSFileReader() {
        logger.info("生成临商账户信息文件批量启动,读取正常的账户信息，机构：{}", ReportingConstant.DEFAULT_ORG_NUMBER);
        return new LSNormalAccountFileReader(sqlSessionFactory, anyTxnBatchProperties.getFileConfig().getPageSize());
    }

    @Bean
    @StepScope
    public ItemReader<String> accountPayoffLSFileReader() {
        logger.info("生成临商账户信息文件批量启动,读取第一次结清的账户信息，机构：{}", ReportingConstant.DEFAULT_ORG_NUMBER);
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        Date today = org.getToday();
        return new LSPayoffAccountFileReader(sqlSessionFactory, anyTxnBatchProperties.getFileConfig().getPageSize(), today);
    }
}
